﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGXEngine.Networking
{
    public class NetworkMessage_NewClientConnected : NetworkMessage
    {
        #region Properties

        public override MessageTypes Type
        {
            get { return MessageTypes.NewClientConnected; }
        }

        public Int64 ClientAccountId
        {
            get { return _clientAccountId; }
            set { _clientAccountId = value; }
        }
        private Int64 _clientAccountId = 0;

        public int CurrentPlayerCount
        {
            get { return _currentPlayerCount; }
            set { _currentPlayerCount = value; }
        }
        private int _currentPlayerCount = 0;

        #endregion

        public NetworkMessage_NewClientConnected()
        {
            this.DestinationInformation.DestinationType = ClientDestinationInfomation.DestinationTypes.AllClients;
        }

        public override int GetMessageSize()
        {
            int size = base.GetMessageSize();

            size += sizeof(Int64); // ClientAccountId
            size += sizeof(int); // CurrentPlayerCount

            return size;
        }

        public override byte[] GetMessageAsBytes()
        {
            List<byte> bytes = new List<byte>(base.GetMessageAsBytes());

            bytes.AddRange(BitConverter.GetBytes(this.ClientAccountId));
            bytes.AddRange(BitConverter.GetBytes(this.CurrentPlayerCount));

            return bytes.ToArray();
        }

        public override int FromBytes(byte[] bytes, int index)
        {
            index = base.FromBytes(bytes, index);

            this.ClientAccountId = BitConverter.ToInt32(bytes, index);
            index += sizeof(Int64);

            this.CurrentPlayerCount = BitConverter.ToInt32(bytes, index);
            index += sizeof(int);

            return index;
        }
    }
}
